package com.yihaodian.cluster.discovery;

import java.util.Collection;

import com.yihaodian.cluster.ClusterState;
import com.yihaodian.cluster.Node;
import com.yihaodian.server.LifeCycleService;

/**
 * join the cluster, discovery of other nodes, publishing of the cluster state
 * to all nodes, electing a master of the cluster that raises cluster state
 * change
 * 
 * startup after all the listener is ready.
 */
public interface NodeDiscovery extends LifeCycleService {

	/**
	 * Get local node
	 * 
	 * @return
	 */
	public Node getLocalNode();
	
	
	/**
	 * read 
	 * 
	 * @return
	 */
	public ClusterState retrieveState(NewClusterStateListener newClusterStateListener)  throws Exception ;

	/**
	 * master node publish ClusterState to other nodes.
	 * 
	 * @param clusterState
	 */
	public void publishState(ClusterState clusterState);

	/**
	 * listening node join/leave
	 */
	public void addListener(InitClusterStateListener listener);

	/**
	 * listening published ClusterState change from master node.
	 * 
	 * @param listener
	 */
	public void addListener(NewClusterStateListener listener);

	/**
	 * listening node join/leave
	 */
	public void addListener(NodeChangeListener listener);

	public interface InitClusterStateListener {
		void initClusterState(ClusterState state);
	}

	public interface NewClusterStateListener {
		void onNewClusterState(ClusterState state);
	}

	public interface NodeChangeListener {
		void onNodeEvent(Collection<Node> nodes);
	}
}
